package com.railway.dao.impl;

import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.railway.bean.Order;
import com.railway.bean.User;
import com.railway.dao.UserDao;
import com.railway.util.DBUtil;

public class UserDaoImpl implements UserDao {

	JdbcTemplate jt = new JdbcTemplate(DBUtil.getDataSource());
	

	@Override
	public User selectUser(String userName, String userPassword) {
		String sql = "select * from user where (user_name = ? and user_password = ?) or (user_phone = ? and user_password = ?) or (user_email = ? and user_password = ?)";
		List<User> list = jt.query(sql, new BeanPropertyRowMapper<User>(User.class),userName,userPassword,userName,userPassword,userName,userPassword);
		if(list.size() == 0) {
			return null;
		}else {
			return list.get(0);
		}
	}


	

	@Override
	public List<Order> selectOrder(int order_id) {
		String sql = "select * from order where order_id = ?";
		List<Order> list = jt.query(sql,new BeanPropertyRowMapper<Order>(Order.class),order_id);
		if(list.size() == 0) {
			return null;
		}else {
			return list;
		}
	}


	

	@Override
	public int selectIsDelete(String userName) {
		String sql = "select isdelete from user where user_name = ? or user_email = ? or user_phone = ?";
		List<User> li = jt.query(sql, new BeanPropertyRowMapper<User>(User.class), userName,userName,userName);
		if(li.size() > 0) {
			return li.get(0).getIsdelete();
		}else {
			return 0;
		}
	}


	@Override
	public int deleteUser(int user_id) {
		String sql = "update user set isdelete = 1 where user_id = ?";
		return jt.update(sql,user_id);
	}


	@Override
	public int selectUserByName(String userName) {
		String sql = "select count(1) from user where user_name = ?";
		return jt.queryForObject(sql,Integer.class, userName);
	}


	@Override
	public int selectUserByEmail(String eamil) {
		String sql = "select count(1) from user where user_eamil = ?";
		return jt.queryForObject(sql,Integer.class, eamil);
	}


	@Override
	public int selectUserByPhoneNum(String phoneNum) {
		String sql = "select count(1) from user where user_phone = ?";
		return jt.queryForObject(sql,Integer.class, phoneNum);
	}


	@Override
	public int insertUser(User user) {
		int sex = 0;
		char ch = (user.getUser_id_num()).charAt(16);
		String temp = Character.toString(ch);
		sex = Integer.parseInt(temp);
		if(sex % 2 == 1) {
			sex = 1;
		}else{
			sex = 0;
		}
		Date date = new Date();
		String sql = "insert into user(user_name,user_password,user_id_type,user_real_name,"
				+ "user_id_num,passenger_type,user_email,user_phone,consumptionPassword,register_time,isdelete)"
				+ " values(?,?,?,?,?,?,?,?,?,?,?)";
		return jt.update(sql,user.getUser_name(),user.getUser_password(),user.getUser_id_type(),
				user.getUser_real_name(),user.getUser_id_num(),user.getPassenger_type(),
				user.getUser_email(),user.getUser_phone(),sex,date,0);
	}


	@Override
	public int selectUserIdByUserName(String user_name) {
		String sql = "select * from user where user_name = ?";
		List<User> li = jt.query(sql, new BeanPropertyRowMapper<User>(User.class), user_name);
		if(li.size() > 0) {
			return li.get(0).getUser_id();
		}else {
			return 0;
		}
	}


	@Override
	public List<User> selectUser(int pageNum, int pageSize) {
		String sql = "select * from user limit ?,?";
		return jt.query(sql, new BeanPropertyRowMapper<User>(User.class),(pageNum-1)*pageSize,pageSize);
	}


	@Override
	public int selectCountUser() {
		String sql = "select count(1) from user";
		return jt.queryForObject(sql, Integer.class);
	}


	@Override
	public String selectUserPassword(int user_id) {
		String sql = "select * from user where user_id = ?";
		List<User> li = jt.query(sql, new BeanPropertyRowMapper<User>(User.class),user_id);
		if(li.size()>0){
			return li.get(0).getUser_password();
		}else{
			return "";
		}
	}


	@Override
	public int updateUserPassWord(int user_id, String user_password) {
		String sql = "update user set user_password = ? where user_id = ?";
		return jt.update(sql, user_password,user_id);
	}


	@Override
	public String selectUserPhone(int user_id) {
		String sql = "select * from user where user_id = ?";
		List<User> li = jt.query(sql, new BeanPropertyRowMapper<User>(User.class),user_id);
		if(li.size()>0){
			return li.get(0).getUser_phone();
		}else{
			return "";
		}
	}


	@Override
	public int updateUserPhone(int user_id, String user_phone) {
		String sql = "update user set user_phone = ? where user_id = ?";
		return jt.update(sql, user_phone,user_id);
	}


	@Override
	public String selectUserEmail(int user_id) {
		String sql = "select * from user where user_id = ?";
		List<User> li = jt.query(sql, new BeanPropertyRowMapper<User>(User.class),user_id);
		if(li.size()>0){
			return li.get(0).getUser_email();
		}else{
			return "";
		}
	}


	@Override
	public int updateUserEmail(int user_id, String user_email) {
		String sql = "update user set user_email = ? where user_id = ?";
		return jt.update(sql, user_email,user_id);
	}


	@Override
	public String selectConsumptionPassword(int user_id) {
		String sql = "select * from user where user_id = ?";
		List<User> li = jt.query(sql, new BeanPropertyRowMapper<User>(User.class),user_id);
		if(li.size()>0){
			return li.get(0).getConsumption_password();
		}else{
			return "";
		}
	}


	@Override
	public int updateConsumptionPassword(int user_id, String consumptionPassword) {
		String sql = "update user set consumptionPassword = ? where user_id = ?";
		return jt.update(sql, consumptionPassword,user_id);
	}




	@Override
	public List<User> selectAllUser(int pageNum, int pageSize) {
		String sql = "select * from user limit ?,?";
		List<User> list = jt.query(sql, new BeanPropertyRowMapper<User>(User.class),(pageNum-1)*pageSize,pageNum);
		return list;
	}




	@Override
	public int addUser(User u) {
		Date date = new Date();
		String sql = "insert into user(user_name,user_password,user_phone,user_real_name,user_id_type,user_id_num,user_email,register_time,passenger_type,consumption_password) values(?,?,?,?,?,?,?,?,?,?)";
		return jt.update(sql,u.getUser_name(),u.getUser_password(),u.getUser_phone(),u.getUser_real_name(),u.getUser_id_type(),u.getUser_id_num(),u.getUser_email(),date,u.getPassenger_type(),u.getConsumption_password());
	}




	@Override
	public User selectByUserName(String userName) {
		String sql = "select * from user where user_real_name = ?";
		List<User> list = jt.query(sql,new BeanPropertyRowMapper<User>(User.class), userName);
		if(list.size() == 0) {
			return null;
		}else {
			return list.get(0);
		}
	}




	@Override
	public int updatePhone(String phoneNum, String userName, String password) {
		String sql = "update user set user_phone = ? where user_name = ? and user_password = ?";
		return jt.update(sql,phoneNum,userName,password);
	}




	@Override
	public int updateEmail(String emailNew, String userName, String password) {
		String sql = "update user set user_email = ? where user_name = ? and user_password = ?";
		return jt.update(sql,emailNew,userName,password);
	}




	@Override
	public int updatePassword(String passwordNew, String userName, String password) {
		String sql = "update user set user_password = ? where user_name = ? and user_password = ?";
		return jt.update(sql,passwordNew,userName,password);
	}




	@Override
	public int updateUserById(int user_id, String user_name, String user_phone, String user_password, String user_email,
			String consumption_password) {
		String sql = "update user set user_name = ?,user_password = ?,user_phone = ?,user_email = ?,consumption_password = ? where user_id = ?";
		return jt.update(sql,user_name,user_password,user_phone,user_email,consumption_password,user_id);
	}

}
